﻿using System;
using System.Windows.Input;
using Microsoft.Win32;
using churchtool.Plugin.SermonUpload.ViewModel;
using System.Collections.Generic;

namespace churchtool.Plugin.SermonUpload.Operations.Commands
{
    class DoBrowseFilesCommand : ICommand
    {
        #region Fields

        // Member variables
        private UploadWindowViewModel m_ViewModel;
        private string SelectedPath;

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public DoBrowseFilesCommand(UploadWindowViewModel viewModel)
        {
            m_ViewModel = viewModel;
        }

        #endregion

        #region ICommand Members

        /// <summary>
        /// Whether the DoBrowseFilesCommand is enabled.
        /// </summary>
        public bool CanExecute(object parameter)
        {
            return true;
        }

        /// <summary>
        /// Actions to take when CanExecute() changes.
        /// </summary>
        public event EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        /// <summary>
        /// Executes the DoBrowseFilesCommand
        /// </summary>
        public void Execute(object parameter)
        {
            SelectedPath = null;

            OpenFileDialog ofd = new OpenFileDialog { Multiselect = false };
            if (ofd.ShowDialog() ?? false)
                SelectedPath = ofd.FileName;

            // Announce that work is starting
            List<string> files = new List<string>();
            files.Add(SelectedPath);

            m_ViewModel.RaiseFilesSelectedEvent(files);
        }

        #endregion
    }
}
